//
//  HCStatusViewModel.swift
//  Hweibome
//
//  Created by 何川 on 15/12/10.
//  Copyright © 2015年 hechuan. All rights reserved.
//

import UIKit

class HCStatusViewModel: NSObject {
    var status: HCStatus? {
        didSet{
            printLog("test")
        }
    }
    
    var vipImage:UIImage?{
        guard let mbrank = status?.user?.mbrank else {
            return nil
        }
        if mbrank>0 && mbrank<7 {
            return UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        return nil
    }
    
    var verifiedImage:UIImage? {
        guard let verified = status?.user?.verified else {
            return nil
        }
        switch verified {
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return nil
        }
    }
    
    var retweetCountStr:String = "转发"
    var commentCountStr:String = "评论"
    var attitudeCountStr:String = "赞"
    var retweetText: String?
    var sourceText: String?
    var createAtText: String{
        guard let createDate = NSDate.sinaDate(status!.created_at!) else {
            return ""
        }
        return createDate.sinaDateString
    }
    
    
    
    init(status:HCStatus)
    {
        super.init()
        self.status = status
        
        retweetCountStr = getCountString(self.status?.reposts_count ?? 0 ,defaulTitle:"转发")
        commentCountStr = getCountString(self.status?.comments_count ?? 0 ,defaulTitle: "评论")
        attitudeCountStr = getCountString(self.status?.attitudes_count ?? 0, defaulTitle: "赞")
        
        if let retweetStatus = status.retweeted_status{
            if let name = retweetStatus.user?.name{
                retweetText = "@\(name):\(retweetStatus.text!)"
            }
        }
        //截取来源文字
        if let source = status.source where source.containsString("\">") {
            let startRange = source.rangeOfString("\">")!
            let endRange = source.rangeOfString("</")!
            sourceText = "来自\(source.substringWithRange(startRange.endIndex..<endRange.startIndex))"
        }
        
    }
    

    
    private func getCountString(count: Int, defaulTitle:String) ->String {
        if count > 0 {
            if count<10000 {
                return "\(count)"
            }else {
                let result = CGFloat(count/1000)/10
                let resultStr = "\(result)万"
                if resultStr.containsString(".0"){
                    return resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                }
                return resultStr
            }
        }
        return defaulTitle
    }
    

    
    override var description: String{
        let keys = ["status"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
}
